package com.framework.service.agent.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.agent.SysShareDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.sys.SysShare;
import com.framework.entity.sys.SysShareImg;
import com.framework.page.PageUtils;
import com.framework.page.Query;
import com.framework.service.AgentService;
import com.framework.service.agent.SysShareImgServcie;
import com.framework.service.agent.SysShareService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 我的文案
 * @author wq
 */
@Service("sysAgentShareService")
public class SysShareServiceImpl extends ServiceImpl<SysShareDao, SysShare> implements SysShareService {

    @Autowired
    private AgentService agentService;
    @Autowired
    private SysShareImgServcie sysShareImgServcie;

    @Override
    public List<SysShare> getShareList(String token, Integer pageNo) {
        SysAgent agent = agentService.queryAgentByToken(token);

        List<SysShare> list = baseMapper.selectPage(new RowBounds((pageNo-1)*5,5),
                new EntityWrapper<SysShare>()
                        .eq("dept_id", agent.getDeptId())
                        .eq("state",1)
                        .orderDesc(Arrays.asList(
                                "create_time","share_date"
                        ))
        );

        list.stream().forEach(e->{
            List<SysShareImg> imgList = sysShareImgServcie.selectList(new EntityWrapper<SysShareImg>().eq("share_id", e.getShareId()));
            e.setImgList(imgList);
        });
        return list;
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long deptId= (Long) params.get("deptId");
        Page<SysShare> page = selectPage(new Query<SysShare>(params).getPage(),
                new EntityWrapper<SysShare>()
                .eq("dept_id",deptId)
        );
        List<SysShare> list = page.getRecords();
        list.stream().forEach(e->{
            List<SysShareImg> imgList = sysShareImgServcie.selectList(new EntityWrapper<SysShareImg>().eq("share_id", e.getShareId()));
            e.setImgList(imgList);
        });
        return new PageUtils(page);
    }

    @Override
    public Integer addShare(SysShare sysShare) {
        Integer count = baseMapper.insert(sysShare);
        System.out.println(count);
        SysShare share = selectOne(new EntityWrapper<SysShare>().eq("content",sysShare.getContent()).eq("share_date",sysShare.getShareDate()).eq("state",sysShare.getState()));
        return share.getShareId();
    }
}
